package com.wjm.blog.common.util.mail;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.wjm.blog.common.util.FileUtil;
import com.wjm.blog.common.util.StringUtil;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.util.Properties;

/**
 * @author jiyuan
 * @version 1.0
 * @date 2022/11/23
 */
public class MailUtil {

    /**
     * 发邮件服务-盘点单
     */
    public static Boolean sendEmail(
            String smtpHost,
            String smtpPort,
            String fromAddress,
            String fromKey,
            String toAddress,
            String title,
            String content,
            byte[] attachmentPart, String fileType) throws Exception {
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.transport.protocol", "smtp");
        // smtp服务器地址
        props.put("mail.smtp.host", smtpHost);

        if (StringUtil.isNotEmpty(smtpPort)) {
            props.put("mail.smtp.port", smtpPort);
        }
        Session session = Session.getInstance(props);
        session.setDebug(true);

        MimeMessage message = new MimeMessage(session);
        message.setSubject(MimeUtility.encodeWord(title));

        // 发件人邮箱(我的163邮箱)
        message.setFrom(new InternetAddress(fromAddress));

        // 收件人邮箱(我的QQ邮箱)
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
        Multipart multipart = new MimeMultipart();
        // 邮件正文
        BodyPart contentPart = new MimeBodyPart();
        contentPart.setContent(content, "text/html;charset=utf-8");
        multipart.addBodyPart(contentPart);

        /******************************************************************************************/
        //附件
        byte[] voAttachmentPart = attachmentPart;
        if (ObjectUtils.isNotEmpty(voAttachmentPart)) {
            File bytes2File = FileUtil.bytes2File(voAttachmentPart, title);
            BodyPart attachmentBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(bytes2File);
            attachmentBodyPart.setDataHandler(new DataHandler(source));
            // 避免中文乱码的处理
            attachmentBodyPart.setFileName(MimeUtility.encodeWord(bytes2File.getName().concat(fileType)));
            multipart.addBodyPart(attachmentBodyPart);
        }
        /********************************************************************************************/
        message.setContent(multipart);
        message.saveChanges();
        Transport transport = session.getTransport();
        // 发件人邮箱,授权码(可以在邮箱设置中获取到授权码的信息)
        transport.connect(fromAddress, fromKey);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

        return true;
    }


    public static void main(String[] args) throws Exception {
//        String str="wd071001<715221856@qq.com>";
//
//        String[] split = str.split("<");
//        String toAddress = split[1].split(">")[0];
//        System.out.println(toAddress);


//        String title = "盘点单-盘点差异结果";
//        String content = "盘点单-盘点差异结果";
//
//        // 附件
//
//        List<QueryStocktakingDiffVo> queryStocktakingDiffVos = new ArrayList<>();
//
//        QueryStocktakingDiffVo queryStocktakingDiffVo = new QueryStocktakingDiffVo();
//        queryStocktakingDiffVo.setPkg("pkg1");
//        queryStocktakingDiffVo.setSku("sku1");
//        queryStocktakingDiffVo.setLot02("lot02");
//        queryStocktakingDiffVo.setAmount(100);
//        queryStocktakingDiffVo.setCheckAmount(120);
//        queryStocktakingDiffVo.setCheckerName("lisi");
//        queryStocktakingDiffVo.setUpdateTimeStr(LocalDateTime.now().toString());
//        queryStocktakingDiffVo.setDiffType("盘盈");
//
//        queryStocktakingDiffVos.add(queryStocktakingDiffVo);
//        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("盘点差异", "盘点差异", ExcelType.XSSF), QueryStocktakingDiffVo.class, queryStocktakingDiffVos);
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        workbook.write(baos);
//        byte[] attachmentPart = baos.toByteArray();
//
//        sendEmail(
//                "smtp.qiye.aliyun.com",
//                "25",
//                "wanjm@redoornetwork.com",
//                "Wjm619223",
//                "wanjm@redoornetwork.com",
//                title,
//                content,
//                attachmentPart,
//                ".xlsx"
//        );


//        Properties props = new Properties();
//        props.setProperty("mail.smtp.auth", "true");
//        props.setProperty("mail.transport.protocol", "smtp");
//        // smtp服务器地址
//        props.put("mail.smtp.host", "smtp.qiye.aliyun.com");
////        props.put("mail.smtp.host", "zmail.zte.com.cn");
//        Session session = Session.getInstance(props);
//        session.setDebug(true);
//
//        MimeMessage message = new MimeMessage(session);
//        message.setSubject("测试发送qq邮箱");
//        //message.setContent(messagePushVo.getContent(),"text/html;charset=UTF-8");
//        // 发件人邮箱(我的163邮箱)
////        message.setFrom(new InternetAddress("li.xin61@zte.com.cn"));
//        message.setFrom(new InternetAddress("wanjm@redoornetwork.com"));
//        // 收件人邮箱(我的QQ邮箱)
//        message.setRecipient(Message.RecipientType.TO, new InternetAddress("wanjm@redoornetwork.com"));
//        Multipart multipart = new MimeMultipart();
//        // 邮件正文
//        BodyPart contentPart = new MimeBodyPart();
//        contentPart.setContent("测试发送邮箱", "text/html;charset=utf-8");
//        multipart.addBodyPart(contentPart);
//        message.setContent(multipart);
//        message.saveChanges();
//        Transport transport = session.getTransport();
//        // 发件人邮箱,授权码(可以在邮箱设置中获取到授权码的信息)
//        transport.connect("wanjm@redoornetwork.com", "Wjm619223");
//        transport.sendMessage(message, message.getAllRecipients());
//        transport.close();
    }

}
